package com.aier.school.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.classes.entity.ClassStudent;
import com.aier.classes.entity.Classes;
import com.aier.classes.service.IClassService;
import com.aier.classes.service.IClassUserService;
import com.aier.common.common.Constant;
import com.aier.common.dao.ICommonDAO;
import com.aier.school.entity.SchoolIcCard;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.service.ISchoolIcCardService;
import com.aier.school.service.ISchoolTeacherService;

@Scope("prototype")
@Component("schoolIcardManageAction")
public class SchoolIcardManageAction extends SchoolBaseAction {

    /**
     * 学校管理-管理员查看的IC卡管理 与 教师查看的IC卡管理 
     */
    private static final long serialVersionUID = 1L;


    @Resource(name = "schoolIcCardService")
    private ISchoolIcCardService schoolIcCardService;
    @Resource(name = "schoolTeacherService")
    private ISchoolTeacherService schoolTeacherService;
    @Resource(name = "classUserService")
    private IClassUserService classUserService;
    @Resource(name = "classService")
    private IClassService classService;   
    
    private int s;                //成功:1 ,失败:0
    private String error_info;    //错误提示信息
        
    private Integer sid;                        //学校id
    private Long tid;                            //教师id
    private Long icid;                            //ic卡id
    private Long csid;                            //学生id
    private Long cid;   // 班级id
    
    private List<SchoolIcCard> schoolIcCardList;// 学校拥有的卡
    private List<String> tnameAccountList;        //教师名字账号列表
    private List<SchoolTeacher> schoolTeacherList;    //学校教师列表
    private List<ClassStudent> classStudentList;    //学校学生列表
    private List<Classes> classList;    //学校班级列表
    
    private List<String> cardNumList;            //卡号列表(所有分派给学校的卡)
    private String account;                        //教师账号                
    private String cardnum;                        //卡号
    private short state = -1;                    //学校卡状态 10:启用,20:注销
    private String icnum;        //卡号(教师添加绑定卡用)
    private String icfoot;        //验证码(教师添加绑定卡用)
    
    private int page;            //页数
    private int page_size;        //每页的记录数
    private int page_count;        //总页数
    private int count;            //总数

    private String sname;   // 学生姓名
    private List<String> cardNumList_unbind;  // 学校未绑定的卡列表
    private String cardnumList;  // 多个卡号（以，号分隔）
    private short identity = -1; // 身份(1学生2教师)
    
    @Override
    public String execute() {
        cardNumList = schoolIcCardService.searchSchoolCardNumList(sid);
        schoolTeacherList = schoolTeacherService.selectTeacher(sid, null, null);
        classStudentList = classUserService.searchClassStudentListBySid(sid);
        classList = classService.searchClassList(sid);
        cardNumList_unbind = schoolIcCardService.searchSchoolUnbindCardNumList(sid);
        
        return SUCCESS;
    }

    //查询学校拥有的IC卡 管理员
    @SuppressWarnings("unchecked")
    public String search_admin() {
        s = 0;
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        try {
            Map<String, Object> map = schoolIcCardService.searchSchoolCardsByCondition(
                    page, page_size,sid,
                    cardnum,state,account);
            if (null != map) {
                schoolIcCardList = (List<SchoolIcCard>) map.get(ICommonDAO.OBJ_LIST);
                if (null != schoolIcCardList) {
                    s = 1;
                }
                page = (Integer) map.get(ICommonDAO.PAGE);
                page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
                page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
                count = (Integer) map.get(ICommonDAO.COUNT);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    //普通教师查询自己的ic卡
    @SuppressWarnings("unchecked")
    public String searchNormal() {
        s = 0;
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        try {
            Map<String, Object> map = schoolIcCardService.searchSchoolCardsByCondition(
                    page, page_size,sid,tid);
            if (null != map) {
                schoolIcCardList = (List<SchoolIcCard>) map.get(ICommonDAO.OBJ_LIST);
                if (null != schoolIcCardList) {
                    s = 1;
                }
                page = (Integer) map.get(ICommonDAO.PAGE);
                page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
                page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
                count = (Integer) map.get(ICommonDAO.COUNT);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }
    
    //普通教师添加IC卡绑定
    public String addTeacherIcard(){
        SchoolIcCard scard = schoolIcCardService.searchSchoolIcCard(icnum,icfoot,sid);
        if(scard==null){
            s = 0;
            error_info = "卡号验证码有误 或 该卡不属于所在学校";
        }else{
            int num = schoolIcCardService.searchBandCard(sid,scard.getIcid());
            if(num>0){
                s = 0;
                error_info = "该卡已经被绑定";
            }else{
                s = schoolIcCardService.updateBindCardTeacher(sid,scard.getIcid(),tid);
            }    
        }
        return SUCCESS;
    }
    
    //修改学校卡状态 (注销、挂失、启用等)
    public String updateSchoolIcardState(){
        s = schoolIcCardService.updateIcardState(icid,state,sid);
        return SUCCESS;
    }
    
    //学校回收ic卡
    public String delete(){
        s = schoolIcCardService.deleteIcard(icid);
        return SUCCESS;
    }
    
    //绑定学生ic卡
    public String bindSchoolIcard_student(){
        int num = schoolIcCardService.searchBandCard(sid,icid);
        if(num>0){
            s = 0;
            error_info = "该卡已经被绑定";
        }else{
            s = schoolIcCardService.updateBindCardStudent(sid,icid,csid);
        }
        return SUCCESS;
    }
    
    //绑定教师ic卡
    public String bindSchoolIcard_teacher(){
        int num = schoolIcCardService.searchBandCard(sid,icid);
        if(num>0){
            s = 0;
            error_info = "该卡已经被绑定";
        }else{
            s = schoolIcCardService.updateBindCardTeacher(sid,icid,tid);
        }
        return SUCCESS;
    }

    /**
     * 通过卡号和学生名绑定学生ic卡
     * @return
     */
    public String bindStudentIcard(){
        // 判断卡是否是该学校的卡
        SchoolIcCard scard = schoolIcCardService.searchSchoolCard(sid, cardnum);
        if (scard == null) {
            s = 2;
            return SUCCESS;
        }
        // 判断卡是否已被绑定
        int num = schoolIcCardService.searchBandCard(sid,scard.getIcid());
        if(num>0){
            s = 0;
            return SUCCESS;
        }
        // 判断该学生是否在该学校中，是否有重名
        classStudentList = classUserService.searchCSBySidName(sid, sname);
        if (csid == null) {
            if (null == classStudentList || classStudentList.size() == 0) {
                s = Constant.num_3;
            } else if (classStudentList.size() > 1) {
                s = Constant.num_4;
            } else {
                s = schoolIcCardService.updateBindCardStudent(sid,scard.getIcid(),classStudentList.get(0).getCsid());
            }
        } else {
            s = schoolIcCardService.updateBindCardStudent(sid,scard.getIcid(),csid);
        }
        
        
        return SUCCESS;
    }
    
    /**
     * 查询学生绑卡记录
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchAddStuIcList() {
        s = 0;
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        try {
            Map<String, Object> map = classUserService.searchAddStuIcList(
                    page, page_size,sid);
            if (null != map) {
                classStudentList = (List<ClassStudent>) map.get(ICommonDAO.OBJ_LIST);
                if (null != classStudentList) {
                    s = 1;
                }
                page = (Integer) map.get(ICommonDAO.PAGE);
                page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
                page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
                count = (Integer) map.get(ICommonDAO.COUNT);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
        
    }
    
    /**
     * 通过卡号和教师名绑定教师ic卡
     * @return
     */
    public String bindTeacherIcard(){
        // 判断卡是否是该学校的卡
        SchoolIcCard scard = schoolIcCardService.searchSchoolCard(sid, cardnum);
        if (scard == null) {
            s = 2;
            return SUCCESS;
        }
        // 判断卡是否已被绑定
        int num = schoolIcCardService.searchBandCard(sid,scard.getIcid());
        if(num>0){
            s = 0;
            return SUCCESS;
        }
        // 判断该教师是否在该学校中，是否有重名
        List<SchoolTeacher> stList = classUserService.searchCTBySidName(sid, sname);
        if (null == stList || stList.size() == 0) {
            s = Constant.num_3;
        } else {
            s = schoolIcCardService.updateBindCardTeacher(sid,scard.getIcid(),stList.get(0).getTid());
        }
        // 获取未绑定的ic卡列表
        cardNumList_unbind = schoolIcCardService.searchSchoolUnbindCardNumList(sid);
        return SUCCESS;
        
    }
    
    /**
     * 查询教师绑卡记录
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchAddTeaIcList() {
        s = 0;
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        try {
            Map<String, Object> map = classUserService.searchAddTeaIcList(
                    page, page_size,sid);
            if (null != map) {
                schoolTeacherList = (List<SchoolTeacher>) map.get(ICommonDAO.OBJ_LIST);
                if (null != schoolTeacherList) {
                    s = 1;
                }
                page = (Integer) map.get(ICommonDAO.PAGE);
                page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
                page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
                count = (Integer) map.get(ICommonDAO.COUNT);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
        
    }
    
    
    // 新版
    
    /**
     * 获取班级的学生列表
     * @return
     */
    public String searchClassStudent() {
        classStudentList = classUserService.searchClassStudentList(cid);
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 班级教师查询班级卡列表
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchClassIccardList() {
        s = 1;
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        try {
            Map<String, Object> map = schoolIcCardService.searchClassCardsByCondition(
                    page, page_size,cid,state);
            if (null != map) {
                schoolIcCardList = (List<SchoolIcCard>) map.get(ICommonDAO.OBJ_LIST);
                page = (Integer) map.get(ICommonDAO.PAGE);
                page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
                page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
                count = (Integer) map.get(ICommonDAO.COUNT);
            }
        } catch (Exception e) {
            e.printStackTrace();
            s = 0;
        }
        return SUCCESS;
    }
    
    /**
     * 通过卡号和学生id绑定学生ic卡
     * @return
     */
    public String bindClassStudentIcard(){
        for (String cn : cardnumList.split(",")) {
            // 判断卡是否是该学校的卡
            SchoolIcCard scard = schoolIcCardService.searchSchoolCard(sid, cn);
            if (scard == null) {
                s = 2;
                return SUCCESS;
            }
            // 判断卡是否已被绑定
            int num = schoolIcCardService.searchBandCard(sid,scard.getIcid());
            if(num>0){
                s = 0;
                return SUCCESS;
            }
            s = schoolIcCardService.updateBindCardStudent(sid,scard.getIcid(),csid);
        }
        // 获取未绑定的ic卡列表
        cardNumList_unbind = schoolIcCardService.searchSchoolUnbindCardNumList(sid);
        
        return SUCCESS;
    }
    
    /**
     * 查询学校所有卡列表(state:-1全部10已分配20已挂失30未分配)
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchAllIccardList() {
        s = 1;
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        try {
            Map<String, Object> map = schoolIcCardService.searchSchoolCardsList(
                    page, page_size,sid,
                    cardnum,state,sname,identity);
            if (null != map) {
                schoolIcCardList = (List<SchoolIcCard>) map.get(ICommonDAO.OBJ_LIST);
                page = (Integer) map.get(ICommonDAO.PAGE);
                page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
                page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
                count = (Integer) map.get(ICommonDAO.COUNT);
            }
        } catch (Exception e) {
            e.printStackTrace();
            s = 0;
        }
        return SUCCESS;
    }
    
    
    
    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setPage_count(int page_count) {
        this.page_count = page_count;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public List<SchoolIcCard> getSchoolIcCardList() {
        return schoolIcCardList;
    }

    public List<String> getTnameAccountList() {
        return tnameAccountList;
    }

    public List<String> getCardNumList() {
        return cardNumList;
    }

    public void setSchoolIcCardService(ISchoolIcCardService schoolIcCardService) {
        this.schoolIcCardService = schoolIcCardService;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    public void setIcid(Long icid) {
        this.icid = icid;
    }

    public void setCsid(Long csid) {
        this.csid = csid;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setCardnum(String cardnum) {
        this.cardnum = cardnum;
    }

    public void setState(short state) {
        this.state = state;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getCount() {
        return count;
    }

    public Long getTid() {
        return tid;
    }

    public List<SchoolTeacher> getSchoolTeacherList() {
        return schoolTeacherList;
    }

    public List<ClassStudent> getClassStudentList() {
        return classStudentList;
    }

    public void setClassUserService(IClassUserService classUserService) {
        this.classUserService = classUserService;
    }

    public void setSchoolTeacherService(ISchoolTeacherService schoolTeacherService) {
        this.schoolTeacherService = schoolTeacherService;
    }

    public void setIcnum(String icnum) {
        this.icnum = icnum;
    }

    public void setIcfoot(String icfoot) {
        this.icfoot = icfoot;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public List<Classes> getClassList() {
        return classList;
    }

    public void setClassService(IClassService classService) {
        this.classService = classService;
    }

    public List<String> getCardNumList_unbind() {
        return cardNumList_unbind;
    }

    public void setCardnumList(String cardnumList) {
        this.cardnumList = cardnumList;
    }

    public void setIdentity(short identity) {
        this.identity = identity;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    
    
}
